package com.lanxin.jet.controllers;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.lanxin.jet.beans.UserBean;
import com.lanxin.jet.services.UserService;
import com.lanxin.jet.tools.JetResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param request
     * @param response
     */
    @RequestMapping(value = "/register.do", method = RequestMethod.POST)
    public Map<String, Object> add(HttpServletRequest request) {
        JetResponse jr = new JetResponse();
        String account = request.getParameter("account");
        if (!account.trim().isEmpty()) {
            if (userService.existAccount(account)) {
                jr.error(JetResponse.RES_FAIL, "账号重名");
            } else {
                String password = request.getParameter("password");
                String name = request.getParameter("name");
                String ip = request.getRemoteAddr();
                jr.success(userService.add(account, password, name, ip));
            }
        } else {
            jr.error(JetResponse.RES_PARAMF, "账号不能为空");
        }
        return jr.map();
    }

    /**
     * 用户登录
     * @param request
     * @param response
     */
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public Map<String, Object> login(HttpServletRequest request) {
        JetResponse jr = new JetResponse();
        String account = request.getParameter("account");
        if (!account.trim().isEmpty()) {
            UserBean user = userService.getAccount(account);
            if (user != null) {
                String password = request.getParameter("password");
                if (user.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
                    jr.success(userService.buildToken(user, request.getRemoteAddr()));
                } else {
                    jr.error(JetResponse.RES_REFUSE, "密码错误");
                }
            } else {
                jr.error(JetResponse.RES_NOTHAS, "账号不存在");
            }
        } else {
            jr.error(JetResponse.RES_PARAMF, "账号不能为空");
        }
        return jr.map();
    }

    /**
     * 获取用户数量
     * @param response
     * @param status
     * @param keyword
     */
    @RequestMapping(value = "/getCount.do", method = RequestMethod.POST)
    public Map<String, Object> getCount(int status, String keyword) {
        JetResponse jr = new JetResponse();
        jr.success(userService.count(status, keyword));
        return jr.map();
    }

    /**
     * 获取用户列表
     * @param response
     * @param offset
     * @param limit
     * @param status
     * @param keyword
     */
    @RequestMapping(value = "/getList.do", method = RequestMethod.POST)
    public Map<String, Object> getList(int offset, int limit, int status, String keyword) {
        JetResponse jr = new JetResponse();
        jr.success(userService.getList(offset, limit, status, keyword));
        return jr.map();
    }

    /**
     * [管理员]修改用户状态
     * @param session
     * @param response
     * @param id
     * @param status
     */
    @RequestMapping(value = "/setStatus.do", method = RequestMethod.POST)
    public Map<String, Object> setStatus(HttpSession session, String id, int status) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_USER)) {
            if (id != null && status != -1) {
                UserBean user = userService.get(id);
                if (user != null) {
                    user.setStatus(status);
                    userService.update(user);
                    jr.success();
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_REFUSE);
        }
        return jr.map();
    }

    /**
     * [管理员]修改用户权限
     * @param session
     * @param response
     * @param id
     * @param role
     */
    @RequestMapping(value = "/setRole.do", method = RequestMethod.POST)
    public Map<String, Object> setRole(HttpSession session, String id, int role) {
        JetResponse jr = new JetResponse();
        if (userService.checkRole(session, UserService.ROLE_USER)) {
            if (id != null) {
                UserBean user = userService.get(id);
                if (user != null) {
                    user.setRole(user.getRole() + role);
                    userService.update(user);;
                    jr.success();;
                } else {
                    jr.error(JetResponse.RES_NOTHAS);
                }
            } else {
                jr.error(JetResponse.RES_PARAMF);
            }
        } else {
            jr.error(JetResponse.RES_REFUSE);
        }
        return jr.map();
    }
}